package com.avic.controller;

import com.avic.common.constant.SystemLogConstant;
import com.avic.common.constant.UserConstant;
import com.avic.common.convertor.MapVideoConvertor;
import com.avic.common.enums.MapVideoStateEnums;
import com.avic.common.util.AssertUtil;
import com.avic.common.util.ex.BizErrorCode;
import com.avic.common.util.lang.ServiceCallBack;
import com.avic.controller.request.CreateMapVideorecordReq;
import com.avic.controller.request.MapVideoListReq;
import com.avic.controller.result.BaseCreateResourceRes;
import com.avic.controller.result.MapVideoListRes;
import com.avic.dal.condition.PageQueryMapVideoListCondition;
import com.avic.dal.model.MapVideo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author huangsheng on 2019/2/27
 */
@Slf4j
@RestController
@RequestMapping("video")
@Api(tags = "视频录制记录管理", description = "视频录制记录管理")
public class MapVideoController extends AbstractFacadeImpl {

    @ApiOperation(value = "新增视频记录")
    @PostMapping
    public BaseCreateResourceRes createMapVideoRecord(@RequestBody CreateMapVideorecordReq requestBody,
                                                      @RequestHeader(value = UserConstant.HEADER_CURRENT_USER_ID_KEY, required = false) String currentUserId) {
        final BaseCreateResourceRes result = new BaseCreateResourceRes();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
                AssertUtil.isNotBlank(requestBody.getVideoSourceName(), "视频名称不能为空");
                AssertUtil.isNotBlank(requestBody.getVideoSourcePath(), "视频路径不能为空");
            }

            @Override
            public void doService() throws Exception {
                MapVideo mapVideo = new MapVideo();
                mapVideo.setCreateUserId(currentUserId);
                mapVideo.setVideoSourceName(requestBody.getVideoSourceName());
                mapVideo.setVideoSourcePath(requestBody.getVideoSourcePath());
                mapVideo.setVideoState(MapVideoStateEnums.LOCAL.getState());
                int resultId = mapVideoService.createMapVideoRecord(mapVideo);
                log.info(" 成功新增视频记录 {}", resultId);

                // 新建系统日志
                systemServiceFeignService.createSystemLog(
                        String.format(SystemLogConstant.CREATE_VIDEO_RECORDS,
                                requestBody.getVideoSourcePath(), requestBody.getVideoSourcePath()), currentUserId);

                result.setItemId(String.valueOf(resultId));
                result.setSuccess(true);
            }
        });
    }

    @ApiOperation(value = "视频记录列表")
    @PostMapping("list")
    public MapVideoListRes mapVideoList(@RequestBody MapVideoListReq mapVideoListReq,
                                        @RequestHeader(value = UserConstant.HEADER_CURRENT_USER_ID_KEY, required = false) String currentUserId) {
        final MapVideoListRes result = new MapVideoListRes();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
                AssertUtil.isTrue(mapVideoListReq.getVideoState() == 1 || mapVideoListReq.getVideoState() == 2,
                        BizErrorCode.PARAM_ERROR, "状态取值错误");
                if (StringUtils.isNotBlank(mapVideoListReq.getStartTime())) {
                    AssertUtil.isTrue(AssertUtil.isValidDate(mapVideoListReq.getStartTime()), BizErrorCode.PARAM_ERROR, "开始时间不是一个标准的时间格式");
                }
                if (StringUtils.isNotBlank(mapVideoListReq.getEndTime())) {
                    AssertUtil.isTrue(AssertUtil.isValidDate(mapVideoListReq.getEndTime()), BizErrorCode.PARAM_ERROR, "结束时间不是一个标准的时间格式");
                }
            }

            @Override
            public void doService() throws Exception {
                PageQueryMapVideoListCondition pageQueryMapVideoListCondition =
                        new PageQueryMapVideoListCondition(mapVideoListReq.getCurrentPage(), mapVideoListReq.getPageSize());
                pageQueryMapVideoListCondition.setEndTime(mapVideoListReq.getEndTime());
                pageQueryMapVideoListCondition.setStartTime(mapVideoListReq.getStartTime());
                pageQueryMapVideoListCondition.setVideoName(mapVideoListReq.getVideoName());
                pageQueryMapVideoListCondition.setVideoState(mapVideoListReq.getVideoState());
                pageQueryMapVideoListCondition.setCreateUserId(currentUserId);

                result.setMapVideoInfos(MapVideoConvertor.mapVideoToMapVideoInfo(
                        mapVideoService.itemList(pageQueryMapVideoListCondition), commonProperties.getViewFilePrefix()));
                result.setTotalRecords(mapVideoService.itemTotalRecords(pageQueryMapVideoListCondition));
                result.setSuccess(true);
            }
        });
    }
}
